/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ViewProcessLog.java
 * 模块说明：	
 * 修改历史：
 * 2006-11-15 - lxm - 创建。
 */
package com.hd123.rumba.action.log;

import java.util.ArrayList;
import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.type.Type;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.log.biz.BProcessResult;
import com.hd123.rumba.log.biz.IBProcessLogMgr;
import com.hd123.rumba.prompt.CPrompt;
import com.hd123.rumba.prompt.IPrompt;
import com.hd123.rumba.prompt.IPromptMgr;
import com.hd123.rumba.prompt.biz.BPrompt;
import com.hd123.rumba.prompt.biz.IBPromptMgr;
import com.hd123.rumba.request.Action;
import com.hd123.rumba.request.ActionAbstractImpl;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.OperateContext;

/**
 * 处理过程日志 | Action
 * 
 * @author lxm
 * 
 */
public class ViewProcessLog extends ActionAbstractImpl implements Action {

  private static final String TEMPLATE_VIEW = "log/ProcessResultView.jsp";
  private static final String TEMPLATE_CONFIRM = "log/ProcessResultConfirm.jsp";
  private static final String TEMPLATE_List = "log/ProcessResultList.jsp";

  @Override
  public String execute() throws Exception {
    String url = ActionConstants.NOTIMPLEMENTED;

    String startnode = (String) dd.getFormValue("start");
    if (startnode == null)
      startnode = "view";
    if ("view".equals(startnode))
      url = this.executeWithPermission("view", null);
    else if ("confirm".equals(startnode))
      url = this.executeWithPermission("confirm", null);
    else if ("viewMore".equals(startnode))
      url = this.executeWithPermission("viewMorePrompts", null);

    return url;
  }

  public String view() throws Exception {
    String processUuid = (String) dd.getFormValue("process_uuid");
    if (processUuid == null)
      throw new Exception("缺少必须的参数process_uuid。");
    String strShowLogs = (String) dd.getFormValue("showLogs");
    Boolean showLogs = new Boolean("true".equalsIgnoreCase(strShowLogs));
    String confirmLink = (String) dd.getFormValue("confirmLink");
    if (confirmLink == null)
      confirmLink = "ViewApplication?start=welcome";
    return showView(processUuid, showLogs, confirmLink);
  }

  public String confirm() throws Exception {
    String processUuid = (String) dd.getFormValue("process_uuid");
    if (processUuid == null)
      throw new Exception("缺少必须的参数process_uuid。");
    String strShowLogs = (String) dd.getFormValue("showLogs");
    Boolean showLogs = new Boolean("true".equalsIgnoreCase(strShowLogs));
    String confirmLink = (String) dd.getFormValue("confirmLink");
    if (confirmLink == null)
      throw new Exception("缺少必须的参数confirmLink。");

    try {
      IBPromptMgr promptMgr = (IBPromptMgr) NamingMgr.getInstance().lookupMgr(IBPromptMgr.class);
      BPrompt bp = promptMgr.getPromptBySender(processUuid);
      promptMgr.readPromptT(bp.getUuid(), new OperateContext(this.getCurrentUser()), bp.getOca());
    } catch (Exception e) {
      dd.addErrorText(e.getMessage());
      return showView(processUuid, showLogs, confirmLink);
    }
    return showConfirm(confirmLink);
  }

  private IBProcessLogMgr bLogMgr = null;

  private IBProcessLogMgr getBLogMgr() throws BusinessException {
    if (bLogMgr == null)
      bLogMgr = (IBProcessLogMgr) NamingMgr.getInstance().lookupMgr(IBProcessLogMgr.class);
    return bLogMgr;
  }

  private String showView(String processUuid, Boolean showLogs, String confirmLink)
      throws Exception {
    BProcessResult result = getBLogMgr().getResult(processUuid);
    if (result == null)
      result = new BProcessResult();

    dd.put("process_result", result);
    dd.put("showLogs", showLogs);
    dd.put("confirmLink", confirmLink);
    dd.setWorkingTemplate(TEMPLATE_VIEW);
    return ActionConstants.APPLICATIONFRAME;
  }

  private String showConfirm(String confirmLink) throws Exception {
    dd.setWorkingTemplate(confirmLink);
    return TEMPLATE_CONFIRM;
  }

  public List getPromptList(String userUuid) throws Exception {
    if (userUuid == null || userUuid.length() == 0)
      throw new Exception("无法找到当前用户的UUID");
    List list = new ArrayList();
    List result = new ArrayList();
    IBPromptMgr promptMgr = (IBPromptMgr) NamingMgr.getInstance().lookupMgr(IBPromptMgr.class);
    try {
      list = promptMgr
          .getSession()
          .find(
              "select p.senderId, p.level, p.message, p.link "
                  + "from "
                  + ((IPromptMgr) NamingMgr.getInstance().lookupMgr(IPromptMgr.class))
                      .lookupClass(IPrompt.class)
                  + " p "
                  + " where p.state = ? and (p.receiverUser.uuid = ?"
                  + " or p.receiverRole.uuid in (select ur.role.uuid from UserRole ur where ur.user.uuid = ?))"
                  + " order by p.level desc, p.lastModified asc", new Object[] {
                  CPrompt.VALUE_STATE_NORMAL, userUuid, userUuid }, new Type[] {
                  Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING });
      if (list != null && list.size() > 0) {
        int iCount = 0;
        for (int i = 0; i < list.size(); i++) {
          iCount++;
          if (iCount > 9)
            break;
          result.add(list.get(i));
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
    return result;
  }

  public String viewMorePrompts() throws Exception {
    dd.setWorkingTemplate(TEMPLATE_List);
    return ActionConstants.APPLICATIONFRAME;
  }

  public void confirm2(String processUuid, long oca) throws Exception {
    try {
      IBPromptMgr promptMgr = (IBPromptMgr) NamingMgr.getInstance().lookupMgr(IBPromptMgr.class);
      BPrompt bp = promptMgr.getPromptBySender(processUuid);
      promptMgr.readPromptT(bp.getUuid(), new OperateContext(this.getCurrentUser()), oca);
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }

}
